;(function ($) {
    /**
     * 获取可领取的优惠券列表
     */
    $.getReceiveCouponList = function () {
        $.ajax({
            url: baseloaction + '/web/uc/op/findForUsersCouponList',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    var couponList = result.entity;
                    if(couponList !=null && couponList.length > 0){
                        //优惠券弹框
                        popCardFn(couponList);
                        $(".coupon-box-btn").click(function () {
                            var _this = $(this);
                            var couponId = _this.data('coupon');
                            //领取优惠券
                            userReceiveCoupon(couponId,1);
                        });
                    }
                }else{
                    icModale(0,'',result.message);
                }
            }
        });
    }

    /**
     * 优惠券弹框
     */
    function popCardFn(couponList) {
        var _ele = '<div class="getCouponEle-head"></div>';
        _ele +='<div class="getCouponEle-content">';
        _ele +='<div class="getCoupon-item-list">';
        $.each(couponList,function (index,coupon) {
            _ele +='<div class="u-coupon__wrap">';
            _ele +='<section class="u-coupon-left u-coupon-zkq">';
            _ele +='<div class="text-center pt10 pb10 pl10 pr10">';
            _ele +='<section class="u-coupon-num mt10">';
            _ele +='<strong class="fs30 c-fff">'+coupon.limitAtion+'</strong>';
            if(coupon.type==1){
                _ele +='<span class="fs18 c-fff">折</span>';
            }else{
                _ele +='<span class="fs18 c-fff">￥</span>';
            }
            _ele +='</section>';
            _ele +='<section class="hLh30">';
            _ele +='<span class="c-fff fs12" title="【满'+coupon.minAmount+'元可用】">【满'+coupon.minAmount+'元可用】</span>';
            _ele +='</section></div></section>';
            _ele +='<section class="u-coupon-cont">';
            _ele +='<div class="ml20 mr10 pt10 pb10">';
            if(coupon.type==1){
                _ele +='<h4 class="pt10"><span class="fs20 c-333">折扣券</span></h4>';
            }else {
                _ele +='<h4 class="pt10"><span class="fs20 c-333">立减券</span></h4>';
            }
            _ele +='<section class="hLh30 mt10 ovh">';
            if(coupon.settingStatus==2){
                _ele +='<span class="c-666">[适用于所有商品]</span>';
            }else{
                _ele +='<span class="c-666">[<a data-coupon="'+coupon.id+'" class="c-info show-shop-box" href="javascript:void(0);">查看使用范围</a>]</span>';
            }
            _ele +='</section>';
            _ele +='<section class="hLh30 ovh">';
            _ele +='<aside class="pull-right">';
            _ele +='<button data-coupon="'+coupon.id+'" class="btn btn-danger btn-rounded btn-outline coupon-box-btn" type="button">领取</button>';
            _ele +='</aside>';
            _ele +='<span class="c-999">';
            if(coupon.timeType==2){
                _ele +='<small class="c-999">永久</small>';
            }else{
                _ele +='<small class="c-999">'+coupon.endTime+'</small>';
            }
            _ele +='</span></section></div>';
            _ele +='<div class="u-coupon-line_wrap"></div>';
            _ele +='</section></div>';
        });
        _ele +='</div></div>';
        parent.layer.open({
            title: false,
            type: 1,
            skin: 'myGetCouponModal', //样式类名
            area: ['480px'],
            scrollbar: false,
            closeBtn: false, //不显示关闭按钮
            shift: 2,
            shadeClose: true, //开启遮罩关闭
            content: _ele,
            btn: ['全部领取','有钱任性，不需要'],
            yes: function(index, layero){
                var couponBox = layero.find('.coupon-box-btn[data-coupon]');
                var couponIds = couponBox.map(function () {
                    return $(this).data('coupon');
                }).get().join(",");
                userReceiveCoupon(couponIds,2);
            }
        });
    }

    /**
     * 领取优惠券
     * @param type 操作类型，1单个领取，2全部领取
     * @param couponIds 领取的优惠ID串
     */
    function userReceiveCoupon(couponIds,type) {
        $.ajax({
            url: baseloaction + '/web/uc/op/couponReceive',
            type:'post',
            dataType:'json',
            data:{
                'couponIds': (couponIds +'')
            },
            success:function (result) {

                if(result.success==true){
                    if(type==1){//单个领取
                        $(".coupon-box-btn[data-coupon='"+couponIds+"']").parents('div.u-coupon__wrap').remove();
                        var list = $(".getCoupon-item-list").find('div.u-coupon__wrap');
                        if(list.length <= 0){
                            parent.layer.closeAll();
                            icModale(0,'',result.message);
                        }
                    }else if(type==2){//全部领取
                        parent.layer.closeAll();
                        icModale(0,'',result.message);
                    }
                }else{
                    icModale(0,'',result.message);
                }
            }
        });
    }
}(jQuery));
